﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crawling;
using System.Globalization;
using MIL.Html;

namespace Site_Extractor
{
	public class ForumHandler
	{
        /*
		public static List<string> GatherAllLinks(string mainLink, Func<string, bool> isMine, bool allowBadChars)
		{
			List<string> res = new List<string>();
            Crawler Crawler = new Crawler(0,0, Encoding.Default.WebName, Common.CrawlUrl.CrawlUrlType.Story);
			var links = Crawler.Crawl();
			foreach (string link in links)
			{
				if (isMine(link) && !res.Contains(link)) res.Add(link);
			}
			return res;
		}
         */
        /*
		public static List<string> GatherAllLinks(string mainLink, Func<string, bool> isMine)
		{
			return GatherAllLinks(mainLink, isMine, false);			
		}
         */ 
		public static List<string> GetFromString(string source, string start, string end)
		{
			List<string> list = new List<string>();

			int s = 0, e = 0;
			string tmp;

			while (true)
			{
				s = source.IndexOf(start, e);
				if (s < 0) break;
				e = source.IndexOf(end, s+start.Length);
				if (e < 0) break;
				tmp = source.Substring(s + start.Length, e - s - start.Length);
				e += end.Length;
				if (list.Contains(tmp)) continue;
				list.Add(tmp);
			}

			return list;
		}
		public static bool GetDate(string dateString, string dateFormat, out DateTime date)
		{
			try
			{
				date = DateTime.ParseExact(dateString, dateFormat, CultureInfo.InvariantCulture);
				return true;
			}
			catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                date = new DateTime(); 
                return false; 
            }
		}
		public static string GetTextFromNode(HtmlNode node)
		{
			return GetTextFromNode(node, false);
		}
		public static string GetTextFromNode(HtmlNode node, bool recursive)
		{
			if (!(node is HtmlElement)) return string.Empty;
			HtmlElement e = node as HtmlElement;
			string output = e.Text;
			if (!recursive) return output;
			foreach (HtmlNode childNode in e.Nodes) output += ' '+GetTextFromNode(childNode, recursive);
			return output;
		}
	}
}
